package com.baizan.daka.ui.activity

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.widget.TextView
import com.baizan.base.BaseActivity
import com.baizan.daka.Constants
import com.baizan.daka.event.HomeEvent
import com.baizan.daka.mvp.model.bean.TaskInfoBean
import com.baizan.daka.mvp.model.bean.UserPackageDes
import com.baizan.daka.net.bean.PublishBean
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.PublishContract
import com.baizan.daka.ui.dialog.ButtomDialog
import com.baizan.daka.ui.presenter.PublishPresenter
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_publish_task.*
import org.greenrobot.eventbus.EventBus

class PublishTaskActivity : BaseActivity<PublishPresenter>(), PublishContract.View {

    var type: Int? = 0
    var taskInfoBean: TaskInfoBean? = null

    companion object {
        fun start(context: Context, userPackageDes: UserPackageDes) {
            val intent = Intent(context, PublishTaskActivity::class.java)
            intent.putExtra("userPackage", userPackageDes)
            context.startActivity(intent)
        }

        fun start(context: Context, taskInfoBean: TaskInfoBean) {
            val intent = Intent(context, PublishTaskActivity::class.java)
            intent.putExtra("info", taskInfoBean)
            intent.putExtra("type", 1)
            context.startActivity(intent)
        }

    }

    override fun initPresenter(): PublishPresenter {
        return PublishPresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.act_publish_task
    }

    override fun initData() {
        submit_bt.setOnClickListener {
            submit()
        }
    }

    private fun submit() {
        val taskClass = if ("抖音".equals(task_source.text.toString())) 1 else 2
        val taskName = task_title_value.text
        taskName ?: let {
            showToast("请输入标题")
            return@let
        }

        val taskMoney = task_money_value.text
        taskMoney ?: let {
            showToast("请输入金额")
            return@let
        }
        val taskType: Int
        when (task_type_value.text.toString()) {
            "点赞" -> taskType = 1
            "关注" -> taskType = 2
            "评论" -> taskType = 3
            "点赞+关注" -> taskType = 4
            "点赞+评论" -> taskType = 5
            else -> taskType = 6
        }
        val star = task_star_value.text.toString().toInt()
        val link = task_link_value.text
        link ?: let {
            showToast("请输入视频链接")
            return@let
        }
        val parckId = Constants.userInfoBean?.packagesInfos?.get(0)?.id
        val publishBean =
            PublishBean(
                taskClass,
                taskName.toString(),
                parckId,
                link.toString(),
                taskType,
                taskMoney.toString().toDouble()
            )
        when (type) {
            0 -> {
                presenter?.publishTask(publishBean)
            }
            1 -> {
                taskInfoBean?.let {
                    presenter?.upOrDown(
                        if (it.enabledFlag == 1) 0 else 1,
                        it.id.toString(),
                        link.toString()
                    )
                } ?: let {
                    showToast("数据异常无法提交")
                }

            }
        }

    }

    override fun initView() {
        var taskInf = intent.getSerializableExtra("info")
        var pagckageInfo = intent.getSerializableExtra("userPackage")
        type = intent.getIntExtra("type", 0)
        task_star_value.isEnabled = false
        taskInf?.let {
            it as TaskInfoBean
            taskInfoBean = it
            task_source_value.text = it.taskClassName
            task_source_value.isEnabled = false
            task_type_value.text = it.taskTypeName
            task_type_value.isEnabled = false
            task_title_value.setText(it.taskName)
            task_title_value.isEnabled = false
            task_star_value.setText(it.totalNumber.toString())
            task_money_value.setText(it.taskPrice.toString())
            task_money_value.isEnabled = false
            task_link_value.setText(it.videoUrl)
            if (it.enabledFlag == 1) {
                submit_bt.text = "下架"
            } else {
                submit_bt.text = "上架"
            }


        } ?: let {
            pagckageInfo?.let {
                it as UserPackageDes
                task_star_value.text = it.likeNumber.toString()
                task_money_value.text = it.unitPrice
            }

        }
        task_source_value.setOnClickListener {
            it as TextView
            val pictureSelectionStr: Array<String> = arrayOf("抖音", "快手")
            val builder: ButtomDialog.Builder = ButtomDialog.Builder(this)
            builder.setContentData(pictureSelectionStr)
            builder.setListener(object : ButtomDialog.OnItemSelectListener {

                override fun OnItemSelected(dialog: Dialog, position: Int, title: String?) {
                    when (position) {
                        2 -> dialog.dismiss()
                        else -> {
                            it.text = title
                            dialog.dismiss()
                        }
                    }
                }
            })
            builder.Build().show()
        }

        task_type_value.setOnClickListener {
            it as TextView
            val pictureSelectionStr: Array<String> =
                arrayOf("点赞", "关注", "评论", "点赞+关注", "点赞+评论", "关注+评论")
            val builder: ButtomDialog.Builder = ButtomDialog.Builder(this)
            builder.setContentData(pictureSelectionStr)
            builder.setListener(object : ButtomDialog.OnItemSelectListener {

                override fun OnItemSelected(dialog: Dialog, position: Int, title: String?) {
                    when (position) {
                        2 -> dialog.dismiss()
                        else -> {
                            it.text = title
                            dialog.dismiss()
                        }
                    }
                }
            })
            builder.Build().show()
        }
    }

    override fun start() {
    }

    override fun publishTaskSuccess() {
        showToast("发布任务成功")
        EventBus.getDefault().post(HomeEvent(0))
        finish()
    }

    override fun upSuccess() {
        showToast("上架成功")
        finish()
    }

    override fun downSuccess() {
        showToast("下架成功")
        finish()
    }

}